## "China's Ideological Spectrum"
## by Jennifer Pan and Yiqing Xu


### Install Packages
## install.packages("foreign")
## install.packages("plyr")

#########################################
##  provincial correlates
#########################################

rm(list=ls(all=TRUE)) 
load("output/result_cfa.RData")
library(foreign)

## provincial means
library(plyr)
lt.prov <- ddply(d, .(provgb), function(x){
    data.frame(lt1 = mean(x$lt1),
               lt2 = mean(x$lt2),
               lt3 = mean(x$lt3))
})


## merging provincial level data
library(foreign)
prov.corr<-read.dta("data/prov_corr.dta")
library(plyr)
prov<-join(prov.corr, lt.prov, by='provgb', type='left', match='all')
prov<- subset(prov, !provgb %in% c(54, 63, 64))

##############################
## Figure 12: first dimension
##############################

plot(prov$lt1, prov$lt2)
plot(prov$lt1, prov$lt3)

pdf("graphs/prov_corr_income.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(9.75,10.75),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(9,11,by=0.1)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(log(prov$income),prov$lt1,pch=16,cex=3, col = "gray80")
lines(lowess(log(prov$income),prov$lt1,f=1), col=1,lwd=5)
mtext("Log Income per capita",1,2.5,cex=1.5);
mtext("Political Liberalism",2,2.5,cex=1.5);
axis(1,cex.axis=1.5)
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

pdf("graphs/prov_corr_trade.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(-5,155),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(0,150,by=12.5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$openness,prov$lt1,pch=16,cex=3, col = "gray80")
lines(lowess(prov$openness,prov$lt1,f=1), col=1,lwd=5)
mtext("Trade openness",1,2.5,cex=1.5);
mtext("Political Liberalism",2,2.5,cex=1.5);
axis(1,cex.axis=1.5,at=seq(0,150,25))
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
text(145,0.10,"Beijing",cex=1.3)
text(135,0.17,"Shanghai",cex=1.3)
text(115,0.21,"Guangdong",cex=1.3)
graphics.off()

pdf("graphs/prov_corr_urban.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(25,92),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(20,90,by=5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$urban,prov$lt1,pch=16,cex=3, col = "gray80")
lines(lowess(prov$urban,prov$lt1,f=1), col=1,lwd=5)
mtext("Urbanization",1,2.5,cex=1.5);
mtext("Political Liberalism",2,2.5,cex=1.5);
axis(1,at=seq(20,90,10),cex.axis=1.5);
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()


################################
## Figure A3: 2nd/3rd dimension
################################

### lt2 

pdf("graphs/prov_corr2_income.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(9.75,10.75),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(9,11,by=0.1)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(log(prov$income),prov$lt2,pch=16,cex=3, col = "gray80")
lines(lowess(log(prov$income),prov$lt2,f=1), col=1,lwd=5)
mtext("Log Income per capita",1,2.5,cex=1.5);
mtext("Pro-Market/Non-Traditional Values",2,2.5,cex=1.5);
axis(1,cex.axis=1.5)
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

pdf("graphs/prov_corr2_trade.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(-5,155),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(0,150,by=12.5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$openness,prov$lt1,pch=16,cex=3, col = "gray80")
lines(lowess(prov$openness,prov$lt1,f=1), col=1,lwd=5)
mtext("Trade openness",1,2.5,cex=1.5);
mtext("Pro-Market/Non-Traditional Values",2,2.5,cex=1.5);
axis(1,cex.axis=1.5,at=seq(0,150,25))
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

pdf("graphs/prov_corr2_urban.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(25,92),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(20,90,by=5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$urban,prov$lt2,pch=16,cex=3, col = "gray80")
lines(lowess(prov$urban,prov$lt2,f=1), col=1,lwd=5)
mtext("Urbanization",1,2.5,cex=1.5);
mtext("Pro-Market/Non-Traditional Values",2,2.5,cex=1.5);
axis(1,at=seq(20,90,10),cex.axis=1.5);
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

### lt3 

pdf("graphs/prov_corr3_income.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(9.75,10.75),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(9,11,by=0.1)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(log(prov$income),prov$lt3,pch=16,cex=3, col = "gray80")
lines(lowess(log(prov$income),prov$lt3,f=1), col=1,lwd=5)
mtext("Log Income per capita",1,2.5,cex=1.5);
mtext("Nationalism * (-1)",2,2.5,cex=1.5);
axis(1,cex.axis=1.5)
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

pdf("graphs/prov_corr3_trade.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(-5,155),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(0,150,by=12.5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$openness,prov$lt3,pch=16,cex=3, col = "gray80")
lines(lowess(prov$openness,prov$lt3,f=1), col=1,lwd=5)
mtext("Trade openness",1,2.5,cex=1.5);
mtext("Nationalism * (-1)",2,2.5,cex=1.5);
axis(1,cex.axis=1.5,at=seq(0,150,25))
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()

pdf("graphs/prov_corr3_urban.pdf")
par(mar=c(4,4,1,1))
plot(1,type="n",ylim=c(-0.4,0.4),xlim=c(25,92),xlab="",ylab="",axes=F);box()
for(i in seq(-1,1,by=0.05)) abline(h=i,col="#AAAAAA50",lwd=1.5)
for(i in seq(20,90,by=5)) abline(v=i,col="#AAAAAA50",lwd=1.5)
points(prov$urban,prov$lt3,pch=16,cex=3, col = "gray80")
lines(lowess(prov$urban,prov$lt3,f=1), col=1,lwd=5)
mtext("Urbanization",1,2.5,cex=1.5);
mtext("Nationalism * (-1)",2,2.5,cex=1.5);
axis(1,at=seq(20,90,10),cex.axis=1.5);
axis(2,at=seq(-1,0.6,0.2),cex.axis=1.3);
graphics.off()
